package com.ruicar.afs.cloud.afscase.apply.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.apply.condition.ApplyBusinessConfCondition;
import com.ruicar.afs.cloud.afscase.apply.condition.RuleAtomCondition;
import com.ruicar.afs.cloud.afscase.apply.config.ApplyConfig;
import com.ruicar.afs.cloud.afscase.apply.fegin.CaseUseApplyServiceFeign;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleInfo;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.channel.config.ChannelApiConfig;
import com.ruicar.afs.cloud.channel.dto.RuleAtomDto;
import com.ruicar.afs.cloud.channel.feign.ChannelApiFeign;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 耿继辉
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/caseBusConf")
@Api("案件业务选择配置管理")
public class BusinessConfController {

    private final CaseUseApplyServiceFeign caseUseApplyServiceFeign;
    private final ApplyConfig applyConfig;
    private final AfsRuleInfoService afsRuleInfoService;
    private final ChannelApiFeign channelApiFeign;
    private final ChannelApiConfig channelApiConfig;

    /**
     * 多条件查询
     *
     * @param condition
     * @return
     */
    @PostMapping("/getBusinessConfInfo")
    public IResponse getBusinessConfInfo(@RequestBody QueryCondition<ApplyBusinessConfCondition> condition) {
        return this.caseUseApplyServiceFeign.getBusinessConfInfo(condition, this.makeApplyHeader());
    }

    /**
     * 根据id获取信息
     *
     * @param condition
     * @return
     */
    @PostMapping("/getInfo")
    @ApiOperation("业务选择配置-根据id获取信息")
    public IResponse getInfo(@RequestBody ApplyBusinessConfCondition condition) {
        return this.caseUseApplyServiceFeign.getInfo(condition, this.makeApplyHeader());
    }

    /**
     * 删除
     *
     * @param condition
     * @return
     */
    @PostMapping("/deleteBusinessConfInfo")
    public IResponse deleteBusinessConfInfo(@RequestBody ApplyBusinessConfCondition condition) {
        return this.caseUseApplyServiceFeign.deleteBusinessConfInfo(condition, this.makeApplyHeader());
    }

    /**
     * 更新
     *
     * @param condition
     * @return
     */
    @PostMapping("/updateBusinessConfInfo")
    public IResponse updateBusinessConfInfo(@RequestBody ApplyBusinessConfCondition condition) {
        return this.caseUseApplyServiceFeign.updateBusinessConfInfo(condition, this.makeApplyHeader());
    }

    /**
     * 新增
     *
     * @param condition
     * @return
     */
    @PostMapping("/saveBusinessConfInfo")
    public IResponse saveBusinessConfInfo(@RequestBody ApplyBusinessConfCondition condition) {
        return this.caseUseApplyServiceFeign.saveBusinessConfInfo(condition, this.makeApplyHeader());
    }


    @PostMapping(value = "/getBrandListByRule")
    @ApiOperation(value = "查询品牌列表")
    public IResponse<Object> getBrandListByRule(@RequestBody RuleAtomDto ruleAtomDto) {
        IResponse iResponse = channelApiFeign.getBrandListByRule(ruleAtomDto, this.makeChannelHeader());
        return new IResponse<Object>().setData(iResponse.getData());
    }

    @PostMapping(value = "/getModelListByRule")
    @ApiOperation(value = "查询车型列表")
    public IResponse<Object> getModelListByRule(@RequestBody RuleAtomDto ruleAtomDto) {
        IResponse iResponse = channelApiFeign.getModelListByRule(ruleAtomDto, this.makeChannelHeader());

        return new IResponse<Object>().setData(iResponse.getData());
    }

    @PostMapping(value = "/getStyleListByRule")
    @ApiOperation(value = "查询款式列表")
    public IResponse<Object> getStyleListByRule(@RequestBody RuleAtomDto ruleAtomDto) {
        IResponse iResponse = channelApiFeign.getStyleListByRule(ruleAtomDto, this.makeChannelHeader());

        return new IResponse<Object>().setData(iResponse.getData());
    }

    /**
     * 获取进件请求头
     *
     * @return
     */
    public Map makeApplyHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", applyConfig.getApplyClientId());
        headers.put("clientSecret", applyConfig.getApllyClientSecret());
        return headers;
    }

    /**
     * 获取渠道请求头
     *
     * @return
     */
    public Map makeChannelHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", channelApiConfig.getChannelClientId());
        headers.put("clientSecret", channelApiConfig.getChannelClientSecret());
        return headers;
    }

    /**
     * 查询规则信息
     *
     * @param condition
     * @return
     */
    @PostMapping("/getRuleInfoByRuleNo")
    @ApiOperation(value = "查询规则信息")
    public IResponse<AfsRuleInfo> getRuleInfoByRuleNo(@RequestBody RuleAtomCondition condition) {
        AfsRuleInfo afsRuleInfo = this.afsRuleInfoService.getOne(Wrappers.<AfsRuleInfo>query().lambda().eq(AfsRuleInfo::getRuleNo, condition.getRuleNo()));
        log.info("规则数据:{}" + afsRuleInfo);
        return IResponse.success(afsRuleInfo);
    }

}
